package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.Result;
import io.renren.modules.canteens.dao.AuxiliaryMateriDao;
import io.renren.modules.canteens.dao.AuxiliaryStatementDao;
import io.renren.modules.canteens.dto.AuxiliaryMateriDTO;
import io.renren.modules.canteens.dto.AuxiliaryMateriDTO;
import io.renren.modules.canteens.dto.SmallCanteenPurchasemxDTO;
import io.renren.modules.canteens.entity.AuxiliaryMateriEntity;
import io.renren.modules.canteens.service.AuxiliaryMateriService;
import io.renren.modules.canteens.service.FoodService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 辅材采购表
 *
 * @author 罗胜平 781757616@qq.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class AuxiliaryMateriServiceImpl extends CrudServiceImpl<AuxiliaryMateriDao, AuxiliaryMateriEntity, AuxiliaryMateriDTO> implements AuxiliaryMateriService {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AuxiliaryMateriDao auxiliaryMateriDao;
    @Override
    public QueryWrapper<AuxiliaryMateriEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AuxiliaryMateriEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Map getOrderList(String createDate, long canteenId, int limit, int page) {
        Map  map =new HashMap<>();
        if(StringUtils.isBlank(createDate)){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            createDate = sdf.format(date);
        }
        String  paging;
        if(0 ==limit){
            paging="";
        }else {
            paging="limit " + (page - 1) * limit + "," + limit;
        }
        createDate = " and  DATE_FORMAT(A.create_date,'%Y-%m')=DATE_FORMAT('"+ createDate +"','%Y-%m') ";
        List<AuxiliaryMateriDTO> orderList = auxiliaryMateriDao.getOrderList(createDate,canteenId,paging);
        AuxiliaryMateriDTO order = auxiliaryMateriDao.getOrdertotalMony(createDate, canteenId);
        AuxiliaryMateriDTO totalPage = auxiliaryMateriDao.getTotalPage(createDate, canteenId);
        if(orderList.size()<1 || null ==order){
            Result result = new Result();
            if(null==createDate){
                result.setMsg("当前月没有数据");
            }
            result.setCode(5001);
            result.setMsg(createDate+"没有数据");
            map.put("result",result);
            return map;
        }
        String name = sysDeptService.get(canteenId).getName();
        SysUserDTO sysUserDTO;
        for (AuxiliaryMateriDTO orderDTO:orderList) {
            orderDTO.setCanteenName(name+"辅材每日进货清单");
            sysUserDTO = sysUserService.get(orderDTO.getChedId());
            if(null !=sysUserDTO){
                orderDTO.setChedName(sysUserDTO.getRealName());
            }
            sysUserDTO = sysUserService.get(orderDTO.getCanteenAdminId());
            if(null !=sysUserDTO){
                orderDTO.setCanteenAdminName(sysUserDTO.getRealName());
            }
        }
        map.put("totalPage",totalPage.getTotalPage());
        map.put("orderList",orderList);
        map.put("totalPrice",order.getTotalPrice());
        return map;
    }
}